package pingan.com.imandtwo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.beans.ValidateCodeBean;
import pingan.com.imandtwo.interfaces.Iview;
import pingan.com.imandtwo.presenter.HttpPresenter;
import pingan.com.imandtwo.utils.BaseActivity;

public class UpdatePsdActivity extends BaseActivity implements Iview {

    @BindView(R.id.land_editText_name)
    EditText landEditTextName;
    @BindView(R.id.land_editText_pwd)
    EditText landEditTextPwd;
    @BindView(R.id.land_editText_pwd2)
    EditText landEditTextPwd2;
    @BindView(R.id.edit_yanzhen)
    EditText editYanzhen;
    @BindView(R.id.yanzhengma)
    Button yanzhengma;
    @BindView(R.id.land_button_land)
    Button landButtonLand;
    private Unbinder unbinder;
    private HttpPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_psd);
        unbinder = ButterKnife.bind(this);

        presenter = new HttpPresenter(this);
    }

    @OnClick({R.id.yanzhengma, R.id.land_button_land})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //修改的验证码
            case R.id.yanzhengma:
                String phone = landEditTextName.getText().toString();
                if (!TextUtils.isEmpty(phone)) {
                    if (isPhone(phone)) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("phoneNumber", phone);
                        map.put("type", "2");
                        presenter.getMap(map, "注册验证码");
                    } else {
                        Toast.makeText(UpdatePsdActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(UpdatePsdActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.land_button_land:

                String userphone = landEditTextName.getText().toString();
                String useryanzhen = editYanzhen.getText().toString();
                String userpsd = landEditTextPwd.getText().toString();
                String userpsd2 = landEditTextPwd2.getText().toString();

                //判空
                if (isNull(userphone, useryanzhen, userpsd, userpsd2)) {
                    if (userpsd.equals(userpsd2)) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("phoneNumber", userphone);
                        map.put("password", userpsd);
                        map.put("password1", userpsd2);
                        map.put("verify", useryanzhen);
                        presenter.getMap(map, "修改密码");
                    } else {
                        Toast.makeText(UpdatePsdActivity.this, "两次密码不一致，请检查，爱你么么哒", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    //手机号判断
    public Boolean isPhone(String username) {
        //正则表达式
        String regex = "^1[3|4|5|7|8]\\d{9}";
        return Pattern.matches(regex, username);
    }


    //输入框判空
    public Boolean isNull(String userphone, String useryanzhen, String userpsd, String userpsd2) {
        if (TextUtils.isEmpty(userphone) || TextUtils.isEmpty(useryanzhen) || TextUtils.isEmpty(userpsd) || TextUtils.isEmpty(userpsd2) ) {
            Toast.makeText(UpdatePsdActivity.this, "有输入框的值为空，请检查！！！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }


    @Override
    public void onSuccess(Object o, String tag) {

        if (tag.equals("注册验证码")) {
            ValidateCodeBean codeBean = (ValidateCodeBean) o;
            if (codeBean.getStatus().equals("0000")) {
                Toast.makeText(UpdatePsdActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
            }
        }

        if (tag.equals("修改密码")) {
            ValidateCodeBean codeBean = (ValidateCodeBean) o;
            Toast.makeText(UpdatePsdActivity.this, codeBean.getMessage(), Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        presenter.disAttach();
    }
}
